<template>
	<page-meta :page-style="'overflow:'+(popShow?'hidden':'visible')"></page-meta>
	<headassy title="费用列表" searchTitle="总金额" @changeTab="changeTab" :tabs="tabs" :tabIndex="tabIndex" @goSearch="goSearch" @openSideBar="getAllSearch"></headassy>
	<view class="money-content">
		<!-- <u-sticky bgColor="#fff">
			<view class="">
				<u-navbar title="费用列表" :autoBack="false" placeholder="true">
					<view class="u-nav-slot" slot="left">
						<u-icon name="arrow-left" size="30" @click="leftClick"></u-icon>
						<u-line direction="column" :hairline="false" length="32" margin="0 8px"></u-line>
						<u-icon name="home" size="42" style="color: #fff;" @click="rightClick"></u-icon>
					</view>
				</u-navbar>
			</view>
			<view class="" style="padding: 20rpx 28rpx;display: flex;background-color: #fff;border-bottom: 1px solid #eee;">
				<view class="" style="flex: 1; margin-right: 28rpx;">
					<u-search placeholder="备件名称" v-model="keyword" height="60" :showAction="false" searchIconSize="48"
						bgColor="#f5f5f5" @search="goSearch" shape="square"></u-search>
				</view>
				<u-icon name="list" size="48" color="#000" @click="openModel('money-sidebar')"></u-icon>
			</view>
			<view class="tabs">
				<u-tabs :current="tabIndex" :list="tabs" lineHeight="6rpx" lineColor="#fd7400"
					:inactiveStyle="{color: '#000'}" :activeStyle="{color: '#fd7400'}" @change="changeTab"
					:scrollable="false"></u-tabs>
			</view>
		</u-sticky> -->
		<view class="nav-status text-orange">
			当前状态有{{count}}个费用单
		</view>
		<scroll-view>
			<view class="card-list">
				<view class="card mt-5" v-for="item in moneylist" :key="item.id">
					<view class="top u-flex-between" @click="goMoneyDetail(item.id)">
						<view class="left u-flex">
							<view class="text-info">
								{{item.applyNo ? item.applyNo : ''}}
							</view>
						</view>
						<view class="right u-flex">
							<view
								:class="{'cus-btn':item.applyStatus==='待审核','cus-btn3':item.applyStatus==='已驳回','cus-btn2':item.applyStatus==='已通过','cus-btn3':item.applyStatus==='已取消'}">
								{{item.applyStatus ? item.applyStatus : ''}}
							</view>
						</view>
					</view>
					<view class="center" @click="goMoneyDetail(item.id)">
						<view class="title u-flex-between">
							<view class="title-left text-gray">
								申请类型:
								<span style="margin-left: 14rpx;">{{item.applyType ? item.applyType : ''}}</span>
							</view>

						</view>
						<view class="text">
							申请网点:
							<span style="margin-left: 14rpx;">{{item.applicant ? item.applicant : ''}}</span>
						</view>
						<view class="text">
							申请金额:
							<span style="margin-left: 14rpx;">￥{{item.applyMoney ? item.applyMoney : ''}}</span>
						</view>
						<view class="text">
							审批金额:
							<span style="margin-left: 14rpx;">￥{{item.checkMoney ? item.checkMoney : ''}}</span>
						</view>
						<view class="text">
							申请时间:
							<span style="margin-left: 14rpx;">{{item.applicationTime ? item.applicationTime : ''}}</span>

						</view>
						<view class="text">
							审核时间:
							<span style="margin-left: 14rpx;">{{item.reviewTime ? item.reviewTime : ''}}</span>
						
						</view>
					</view>
					<view class="down u-flex-content-end">
						<view class="right u-flex">
							<view class="cus-blue-btn" @click="goWorkList(item)">
								工单
							</view>
							<view class="cus-oragen-btn" v-if="item.applyStatus==='待审核'" style="margin-left: 12px;" 
							@click="goSh(item)">
								审核
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="example-body">
				<uni-load-more :status="status" :content-text="contentText"/>
			</view>
		</scroll-view>
		
		<!-- 侧边栏 -->
		<sidebarpop
			ref="page-sidebar"
			ptitle="筛选"
			:sidebarList="JSON.stringify(sidebarList)"
			@closeclick="closePop"
			@radioClick="radioClick"
			@clearSelect="clearSelect"
			@saveclick="saveSelect"
		>
		</sidebarpop>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabIndex: 0,
				tabs: [
					{
						name: '待审核',
						disabled: false,
						param: {
							applyStatus: '待审核'
						}
					},
					{
						name: '已通过',
						disabled: false,
						param: {
							applyStatus: '已通过'
						}
					},
					{
						name: '已驳回',
						disabled: false,
						param: {
							applyStatus: '已驳回'
						}
					}, {
						name: '已取消',
						disabled: false,
						param: {
							applyStatus: '已取消'
						}
					},
					{
						name: '全部费用',
						disabled: false,
						param: {
							applyStatus: ''
						}
					},
				],
				page: {
					pageNum: 1,
					pageSize: 20,
					custAccId: '',
				},
				data: {
					applyStatus: '',
					applyType: '',
					checkEmpId: '',
					networkId: '',
				},
				newParams: [],
				count: 0,
				moneylist: [],
				moneyTypeList: [],			// 费用类型
				kfselect: [],				// 客服数据
				wdselect: [],				// 网点数据
				keyword: '',
				
				status: 'loading',		//上拉加载
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				
				sidebarList: [],			// 侧边栏数据
				popShow: false,				// 弹窗触发时为true
				system: uni.getSystemInfoSync(),
			};
		},
		onLoad(e) {
			this.page.custAccId = uni.getStorageSync('custAccid');
			if (e.typeName) {
				for (var i = 0; i < this.tabs.length; i++) {
					if (this.tabs[i].name === e.typeName) {
						this.tabIndex = i;
						this.data.applyStatus = this.tabs[i].param.applyStatus;
						break;
					}
				}
			}
			this.sidebarInit();
			this.getPullRefresh();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getPullRefresh()
		},
		// 触底监听
		onReachBottom() {
			console.log('触底,判断数据余量,更新数据');
			console.log('列表分组总数:'+Math.ceil(this.count / 20),'当前列表:'+this.page.pageNum);
			if(this.page.pageNum < Math.ceil(this.count / 20)) {
				this.status = 'loading';
				this.page.pageNum++;
				this.getPageList('refresh');
			}else {
				this.status = 'noMore';
				// this.$alertTost('到底了~')
			}
		},
		methods: {
			// 初始化数据
			async sidebarInit() {
				const sidebarLable = ['费用状态','全部类型','客服','网点'];
				const sidebarList = [this.tabs,this.moneyTypeList,this.wdselect,this.kfselect];
				this.sidebarList = [];
				console.log(this.tabIndex)
				sidebarLable.forEach((val,index) => {
					this.sidebarList.push({
						selectEle: null,
						isShowList: false,
						title: val,
						isModelShow: index == 0 ? this.tabIndex == (this.tabs.length - 1) : true,
						data: this.handleSidebarData(sidebarList[index])
					});
				})
				console.log('侧边栏预处理数据----',this.sidebarList);
			},
			handleSidebarData(res = []) {
				let data = res;
				data.forEach(val => {
					val.ischeck = false;
					if(!val.title) {
						if(val.nameVal) {
							val.title = val.nameVal
						}
						if(val.name) {
							val.title = val.name
						}
					}
				})
				return data
			},
			
			// 下拉刷新请求数据
			getPullRefresh(){
				console.clear()
				this.getPageList();
				this.getServiceFees();
				this.findemp();
				this.findwd();
			},
			// 获取费用列表
			async getPageList(type) {
				this.status = 'loading';
				this.count = 0;
				if(type != 'refresh') {
					this.moneylist = [];
				}
				const res = await uni.$api.work.getMoneyList({
					...this.page,
					...this.data
				})
				this.count = res.count;
				this.status = 'noMore';
				
				if(type == 'refresh') {
					this.moneylist.push(...res.data);
				}else {
					this.moneylist = res.data
				}
				// 回到顶部
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 200
				});
				uni.stopPullDownRefresh()
			},
			// 获取费用全部类型
			async getServiceFees() {
				const res = await uni.$api.money2.getServiceFees({
					custAccId: this.page.custAccId
				})
				this.moneyTypeList = res.data.map((val => {
					val.ischeck = false;
					val.label = val.title
					return val;
				}));
			},
			// 获取客服数据
			async findemp() {
				const res = await uni.$api.work.findwd({
					custAccId: this.page.custAccId
				})
				this.kfselect = res.data.map((val => {
					val.ischeck = false;
					val.label = val.title
					return val;
				}));
			},
			// 获取网点数据
			async findwd() {
				const res = await uni.$api.work.findemp({
					custAccId: this.page.custAccId
				})
				this.wdselect = res.data.map((val => {
					val.ischeck = false;
					val.label = val.title
					return val;
				}));
			},
			
			// 切换选项卡
			changeTab(item) {
				this.tabIndex = item.index;
				this.data.applyStatus = item.param.applyStatus;
				this.page.pageNum = 1;
				this.getPageList()
			},
			// 去工单详细
			goWorkList(item) {
				console.log(item, '工单详细')
				uni.navigateTo({
					// workId
					url: `../../pages/worklist/workdetail?workId=${item.workId}`
				})
			},
			// 去费用详情
			goMoneyDetail(id) {
				uni.navigateTo({
					url: `./moneydetail?id=${id}`
				})
			},
			// 去审核费用
			goSh(item){
				console.log(item, 'item')
				uni.navigateTo({
					url:`/pagesA/pages/money/auditfees?id=${item.id}`
				})
			},
			
			// 打开侧边栏
			getAllSearch() {
				this.openModel('page-sidebar');
				this.sidebarInit();
			},
			
			// 搜索
			goSearch(e) {
				// this.data.singleNum = e;
				// this.saveSelect();
				console.log('接口更新中...')
			},
			
			// 关闭弹窗
			closePop() {
				this.popShow = false;
			},
			// 新开弹窗
			openModel(ref) {
				if(this.$refs[ref]) {
					this.popShow = true;
					this.$refs[ref].pupshow = true;
				}
			},
			// 侧边栏模块标签点击
			radioClick(item,index,isRepeat) {
				console.log(item,index,isRepeat)
				switch (index) {
					case 0: // 工单类型
						this.data.applyStatus = '';
						if(isRepeat) {
							break;
						}
						this.data.applyStatus = item.title
					case 1: // 工单状态
						this.data.applyType = isRepeat ? '' : item.title
						break;
					case 2:
						this.data.checkEmpId = isRepeat ? '' : item.id
						break;
					case 3:
						this.data.networkId = isRepeat ? '' : item.id
						break;
				}
			},
			
			// 清除数据
			clearSelect() {
				Object.keys(this.data).forEach(key => {
					this.data[key] = ''
				})
				this.getPageList()
			},
			
			// 确认筛选
			saveSelect() {
				this.page.pageNum = 1;
				this.getPageList();
				this.closePop();
			},
		},

	}
</script>

<style lang="scss">
	// 去掉默认背景色
	/deep/.u-popup__content {
		background-color: transparent !important;
	}
	// 去掉顶栏固定
	/deep/.u-navbar--fixed {
		position: static !important;
	}
	.money-content {
		// height: 100vh;
		background-color: $uni-bg-color-grey;

		.u-nav-slot {
			display: flex;
			align-items: center;
		}

		.tabs {
			background-color: #fff;
			font-size: 28rpx;
			width: 100%;
		}

		.nav-status {
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 28rpx;
		}

		.card-list {
			background: $uni-bg-color-grey;
			padding: 0 20rpx;

			.card {
				padding: 28rpx 32rpx;
				background: #fff;
				border-radius: 10rpx;
				margin-bottom: 28rpx;

				.top {
					border-bottom: solid 1px #f5f5f5;
					padding-bottom: 10px;

					.left {
						.text-info {
							font-size: 28rpx;
							font-weight: 700;
						}
					}
				}

				.center {
					padding-top: 10px;
					padding-bottom: 10px;

					.title {
						.title-left {
							font-size: 28rpx;
						}

						.title-right {
							font-size: 28rpx;
						}
					}

					.text {
						color: gray;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}

				.down {
					border-top: solid 1px #f5f5f5;
					padding-top: 12px;
				}
			}
		}
	}
</style>